package com.micai.springboot.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micai.springboot.dto.sys.SysMsgSendDto;
import com.micai.springboot.entity.sys.SysMsgSendEntity;
import com.micai.springboot.mapper.sys.SysMsgSendMapper;
import com.micai.springboot.service.SysMsgSendService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/10/26 15:10
 * @Description:
 */
@Service
@Transactional
public class SysMsgSendServiceImpl extends ServiceImpl<SysMsgSendMapper, SysMsgSendEntity> implements SysMsgSendService {

    @Autowired
    private SysMsgSendMapper msgSendMapper;

    @Override
    public Page<SysMsgSendDto> findPage(int pageNum, int pageSize, String status) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> columnMap = new HashMap<String, Object>(2);
        if (!StringUtils.isEmpty(status)) {
            columnMap.put("status", status);
        }
        Page<SysMsgSendEntity> sysMsgSendEntities = msgSendMapper.findPage(columnMap);
        if (CollectionUtils.isEmpty(sysMsgSendEntities)) {
            return null;
        }
        Page<SysMsgSendDto> sysMsgSendDtos = new Page<SysMsgSendDto>();
        for (SysMsgSendEntity sysMsgSendEntity : sysMsgSendEntities) {
            SysMsgSendDto sysMsgSendDto = new SysMsgSendDto();
            BeanUtils.copyProperties(sysMsgSendEntity, sysMsgSendDto);
            sysMsgSendDtos.add(sysMsgSendDto);
        }
        return toDto(sysMsgSendEntities, sysMsgSendDtos);
    }

    private Page<SysMsgSendDto> toDto(Page<SysMsgSendEntity> sysMsgSendEntities, Page<SysMsgSendDto> sysMsgSendDtos) {
        BeanUtils.copyProperties(sysMsgSendEntities, sysMsgSendDtos);
        return sysMsgSendDtos;
    }

    @Override
    public Integer save(SysMsgSendDto sysMsgSendDto) {
        SysMsgSendEntity sysMsgSendEntity = new SysMsgSendEntity();
        BeanUtils.copyProperties(sysMsgSendDto, sysMsgSendEntity);
        return msgSendMapper.insert(sysMsgSendEntity);
    }
}
